var sh;
var lv1_Array=[];
var lv2_Array=[];
var lv3_Array=[];
var lv4_Array=[];
var lv_Arr=[];
var lv2_menu;	
var lv4_menu;
var lv4;
var lv3width;
var oldPid;
var newPid;
var coldPid;
var cnewPid;
var str;
var theId;
//滚动
var stateTimer; //滚动菜单计时
var navlength=0; //一级菜单子节点总
var navWidth=0; //菜单宽度
var maxWidth=0; //最大宽度
var pageWidth=0;
var marginLeftWidth=76;
var	origLeft="0px";
var navArr=new Array();
var tarDis;
var animeTimer;
var step=150;	//移动步长，应为一个菜单项宽度
var second=0;  //动画时间 1000=1秒 
var sw=0;
var mObj;
var mUl;
var mArr=[];
var leftArrow;	
var rightArrow;
var scrollFlag;
var inMenuBgFlag;

function setFrameHeight(){
	var sh;
	if($('.in_menu_bg').css('display')!='none'){
	   sh=$(window).height()-123;		
	   $('#bottomFrame').height(sh);
	}
	else{
	  sh=$(window).height()-41;		
	  $('#bottomFrame').height(sh);
	}
}

window.onresize=function(){
	setFrameHeight();	
	regetWidth(theId);			
}

//菜单
function formatJson(_data){
	var i;	
	var obj=_data;
	var objLen=obj.length;
	var nodeLength;
	var maxLv=0;	
	var currNodeMenu;
	var currNode;
	var lv1;
	var lv2;
	var lv3;
	
	//循环节点	
	//构建一级
	
	$(obj).each(function(index){				
		var lv1_id=obj[index].id;					
		var lv1_href="javascript:void(0)";
		var lv1_url=(obj[index].url=="#" || obj[index].url=="" )? " " : obj[index].url;
		var lv1_name=obj[index].name;
		var lv1_menu;
		var lv1_modulelvl;
		var icon;
		
		//增加选中
		if(index==0){
			//<li class="selected"><a href="javascript:void(0)">我的网银</a></li>
			lv1_menu="<li issamewin=\""+obj[index].issamewin+"\" class=\"selected\" id=\"" + lv1_id + "\" url=\"" + lv1_url+ "\" name=\"" + lv1_name + "\"><div class=\"bg\"><a href=\"" + lv1_href + "\"" +  ">" + lv1_name + "</div></a></li>";
			theId=0;	
		}
		else{
			lv1_menu="<li issamewin=\""+obj[index].issamewin+"\" id=\"" + lv1_id + "\" url=\"" + lv1_url+ "\" name=\"" + lv1_name + "\"><div class=\"bg\"><a href=\"" + lv1_href + "\"" +  ">" + lv1_name + "</div></a></li>";						
		}
		
		//生成html					
		lv1_Array.push({lv1:lv1_menu});
		$('#TopMenu').append(lv1_Array[index].lv1);				
		
	});
	
	//构建二级
	$('#TopMenu>li').each(function(index) {  
							
		lv1=$(this); 		
		
		if(obj[index].nodes){							
			currNodeMenu=obj[index].nodes; 					
			$('#InMenu').append("<ul id=\"in_menu_ul" + index + "\" class=\"in_menu_ul\"></ul>");
			lv2=$($("#InMenu").find('.in_menu_ul'))[index];
			//alert($(lv2).attr('id'));
			//隐藏						
			if(index==0){
				$(lv2).show();
			}
			else{
				$(lv2).hide();
			}
			
			lv2_Array=[];					
			
			$(currNodeMenu).each(function(index2) {				
				//console.log(obj[index].nodes[index2].name);						
				var lv2_id=currNodeMenu[index2].id;							
				var lv2_href="javascript:void(0)";
				var lv2_url=(currNodeMenu[index2].url=="#" || currNodeMenu[index2].url=="" )? " " : currNodeMenu[index2].url;
				var lv2_name=currNodeMenu[index2].name;
				lv2_menu="";
				var lv3_img;
				
				//二级菜单属性				
				
				lv2_menu="<li><div class=\"bg\"><div class=\"lv2\"><ul>";
				$(obj[index].nodes[index2].nodes).each(function(index3) {  
				  var lv3Node=obj[index].nodes[index2].nodes[index3];
				  var lv3_id=lv3Node.id;							
				  var lv3_href="javascript:void(0)";
				  var lv3_url=(lv3Node.url=="#" || lv3Node.url=="" )? " " : lv3Node.url;
				  var lv3_name=lv3Node.name;				  
				  var lv3_class;
				  if(lv3Node.wordclass){
				 	 lv3_class=(lv3Node.wordclass=="")? "word" : lv3Node.wordclass; 
				  }
				  else{
					  lv3_class="word"; 
				  }
				  if(lv3Node.name.length <= 4){
				  	lv3_class="word";
				  }else if(lv3Node.name.length >4&& lv3Node.name.length <8){
				  	lv3_class = "word6"
				  
				  }else if( lv3Node.name.length <=8){
				  	lv3_class = "word8"
				  }
				  lv3_img=(lv3Node.icon=="")?"common/images/icon/0001.gif":lv3Node.icon;
				  //console.log(obj[index].nodes[index2].nodes[index3].id)
				  lv2_menu+="<li issamewin=\""+lv3Node.issamewin+"\" class=\"lv2menu\" id=\"" + lv3_id + "\" url=\"" + lv3_url+ "\" name=\"" + lv3_name + "\"><div class=\"bg\"><div class=\"icon\"><img src=" +lv3_img +" width=\"32\" height=\"32\" /></div><div class=\""+ lv3_class + "\"><p><a href=\"" + lv3_href + "\" title=\""+ lv3_name +"\">" + lv3_name + "</a></p></div></div>";
				  
				  var _pid=obj[index].nodes[index2].nodes[index3].id;
				  var _json=obj[index].nodes[index2].nodes[index3];
				  //console.log("-----"+ _pid +_json.name+"----------");								
				  lv4_menu="";
				  lv2_menu+=getNodes(_pid,_json, lv2_menu);									
				
				});	
				
				lv2_menu+="</ul></div><div class=\"lv1\"><p><a href=\"" + lv2_href + "\">" + lv2_name + "</a></p></div></div></li>";
				lv2_Array.push({lv2:lv2_menu});							
				$(lv2).append(lv2_Array[index2].lv2);
				//console.log('-----'+$(lv2).html());										
			});	
					
		}				
		
		//一级菜单事件
		$(lv1).bind('click',function(){
						
			$('#TopMenu>li').each(function(){
				$(this).removeClass('selected');							
			});	
			
			$('#InMenu>ul').each(function(index) {
				$(this).hide();
			});				
			
			$($('#InMenu').find('.in_menu_ul')[index]).show();
			$(this).addClass('selected');				
			
		});	
		//二级浮动效果
		$('.lv2menu').hover(
		  function () {
			$(this).addClass("selected");
			$(this).find('.lv3').show();
		  },
		  function () {
			$(this).removeClass("selected");
			$(this).find('.lv3').hide();
		  }
		);
		$('.lv3>li').hover(
		  function () {
			$(this).addClass("selected");
			$(this).find('.submenu').first().show();
		  },
		  function () {
			$(this).removeClass("selected");
			$(this).find('.submenu').first().hide();
		  }
		);	
		$('.submenu>li').hover(
		  function () {
			$(this).addClass("selected");
			$(this).find('.submenu').first().show();
		  },
		  function () {
			$(this).removeClass("selected");
			$(this).find('.submenu').first().hide();
		  }
		);
		
		
	});
	
	//点击事件		
	$('.top_menu_ul>li').bind('click',function(){			
		//changeIframeSrc($(this).attr('url'),$(this).attr('id'),$(this).attr('name'));
		if($('.in_menu_bg').css('display')=='none'){
			$('.in_menu_bg').show();
			switchUpDown();			   
		    $('.logo_pic').toggle();
			$('.logo_pic2').toggle();
			regetWidth($(this).index());
		 	
		}
		else{
		regetWidth($(this).index());
		
		}
				
	});		
	$('.lv2menu').bind('click',function(){
		changeIframeSrc($(this).attr('url'),$(this).attr('id'),$(this).attr('name'),$(this).attr('issamewin'));
	});
	$('.lv3>li').bind('click',function(){
		changeIframeSrc($(this).attr('url'),$(this).attr('id'),$(this).attr('name'),$(this).attr('issamewin'));
	});
	$('.submenu>li').bind('click',function(){
		changeIframeSrc($(this).attr('url'),$(this).attr('id'),$(this).attr('name'),$(this).attr('issamewin'));
	});
	
	
}

//获得4级菜单
function getNodes(_pid,_json, _obj){

newPid=_pid;
html="";
if(_json.nodes != "" && _json.nodes != undefined){	

	var j=_json.nodes;		
	var _url="javascript:void(0)";	
	var lv4Node;
	var lv4_id;							
	var lv4_href;
	var lv4_url;
	var lv4_name;					             							    
	var lv4_img;	
	
	for(var i=0;i<j.length;i++){
   
		if(oldPid!=newPid){
			oldPid=newPid;
			//console.log('ul---');
			lv4_menu="<ul class=\"lv3\" style=\"display:none\">";
		}	
			
		//console.log(j[i].name);		
		lv4Node=j[i];
		lv4_id=lv4Node.id;							
		lv4_href="javascript:void(0)";
		lv4_url=(lv4Node.url=="#" || lv4Node.url=="" )? " " : lv4Node.url;
		lv4_name=lv4Node.name;					             							    
		lv4_img=(lv4Node.icon=="")?"common/images/icon/0001.gif":lv4Node.icon;
	
		if(j[i].nodes.length){			
			
			//lv4_menu+="<li id=\"" + lv4_id + "\" url=\"" + lv4_url+ "\" name=\"" + lv4_name + "\"><a href=\"javascript:void(0)\"><i>"+lv4_name+"<span class=\"arrow\"></span></i></a>"
			lv4_menu+="<li><a href=\"javascript:void(0)\"><i>"+j[i].name+"<span class=\"arrow\"></span></i></a>";
			//返回4级以后的菜单
			lv4_menu+=getChildNodes(_pid,j,lv4_menu);					
		}
		else{
			lv4_menu+="<li issamewin=\""+lv4Node.issamewin+"\" id=\"" + lv4_id + "\" url=\"" + lv4_url+ "\" name=\"" + lv4_name + "\"><a href=\"javascript:void(0)\"><i>"+lv4_name+"</i></a>";
			//lv4_menu+="<li><a href=\"javascript:void(0)\"><i>"+j[i].name+"</i></a>"
		}			
		
	}	
	
	lv4_menu+="</li></ul>";
	html+=lv4_menu;
	//console.log('--/ul--');	
}
return html;	
}
var curr_lvmenu;

//console.log(str);
var lvNode;
var lv_id;							
var lv_href;
var lv_url;
var lv_name;					             							    
var lv_img;	
var lv_menu="";

function getChildNodes(_pid,_json,_str){
	
var _url="javascript:void(0)";	
cnewPid=_pid;


for(var i=0;i<_json.length;i++){
	    //if(_json[i].nodes != "" && _json[i].nodes != undefined){	
		//console.log(i);
		//console.log(_json.length);		
	//console.log(_json[i].nodes);
	if(_json[i].modulelvl==3){

	   if( _json[i].nodes.length){		
	   
		  for(j=0;j<_json[i].nodes.length;j++){
				
			if(coldPid!=cnewPid){
			 	 
			  coldPid=newPid;
			  str="";
			  lv_menu="<ul class=\"submenu\" style=\"display:none\">"
			  //console.log('ul2---');
			}
			
			lvNode=_json[i].nodes[j];
			lv_id=lvNode.id;							
			lv_href="javascript:void(0)";
			lv_url=(lvNode.url=="#" || lvNode.url=="" )? " " : lvNode.url;
			lv_name=lvNode.name;					             							    
			lv_img=(lvNode.icon=="")?"common/images/icon/0002.gif":lvNode.icon;
			//console.log("第二次"+ _json[i].nodes[j].name +"子集"+_json[i].nodes[j].modulelvl);
			
			if(_json[i].nodes[j].nodes.length){		
				
				lv_menu+="<li  id=\"" + lv_id + "\" url=\"" + lv_url+ "\" name=\"" + lv_name + "\"><a href=\"javascript:void(0)\"><i>"+lv_name +"<span class=\"arrow\"></span></i></a>"
				var json1 = _json[i].nodes[j].nodes;					   
				getChildNodes(_pid,json1,str);
			}
			else{
				
				lv_menu+="<li><a href=\"javascript:void(0)\"><i>"+_json[i].nodes[j].name +"</i></a>"
			}
			
		  }
	   }	 
	   str+=lv_menu+"</li></ul>";
	}
	else{		
	
	   lv_menu+="<ul class=\"submenu\" style=\"display:none\">"; 	
	   if(_json[i].nodes.length){	
	   
		  //console.log(_json[i].nodes);
		  lv_menu+="<li  id=\"" + _json[i].id + "\" url=\"" + lv_url+ "\" name=\"" + _json[i].name + "\"><a href=\"javascript:void(0)\"><i>"+_json[i].name +"<span class=\"arrow\"></span></i></a>"
		      var json1 = _json[i].nodes;					   
			  getChildNodes(_pid,json1,str);
		  }
		  else{		
		  
		  lv_menu+="<li><a href=\"javascript:void(0)\"><i>"+_json[i].name +"</i></a>"
	   }
 	   lv_menu+="</li></ul>";
		
	}
	 //console.log(str);		
	   //lv_menu+="</li></ul>"
	   //console.log(lv_menu);
	   
	   //console.log('--/ul2-');
	//}
}
return str;	

}
//滚动条
/**
/* 菜单滚动
/* 2012-4-6
**/
//stateTimer=setTimeout("getMenuState()",50);	


//获取菜单加载状态
function getMenuState(){	
//alert(document.getElementById("nav1").getElementsByTagName("li").length);	
navlength=document.getElementById("nav1").getElementsByTagName("li").length;
if(navlength>0){	
	//alert(navlength);
	getWidth();	//获取宽度
}
else{
	stateTimer=setTimeout("getMenuState()",50);	
}
}

//计算菜单宽度
function getWidth(){

/*pageWidth=(document.body.clientWidth>document.documentElement.clientWidth)?document.body.clientWidth:document.documentElement.clientWidth;*/
pageWidth=$(window).width();

mObj=document.getElementById("InMenu");
var mLen=mObj.childNodes;

for(var i=0;i<mLen.length;i++){
	var tmp=mObj.childNodes.item(i);
	var tmpLen;
	var tmpNode;
	var nodeWidth=0;		
	mArr.push({ID:tmp.id,CSS:tmp.className,DISPLAY:tmp.style.display,WIDTH:nodeWidth});
	
	if(mArr[i].DISPLAY!="none"){
	  var tmpLen=document.getElementById(mArr[i].ID).childNodes.length
	  var tmpNode=document.getElementById(mArr[i].ID).childNodes;
	  //计算图标菜单宽度				
	  for(var j=0;j<tmpLen;j++){			
		 nodeWidth+=tmpNode.item(j).offsetWidth;			 
	  }
	  nodeWidth+=marginLeftWidth;
	  mArr[i].WIDTH=nodeWidth+marginLeftWidth;
	  navWidth=mArr[i].WIDTH;
	  maxWidth=mArr[i].WIDTH-pageWidth;		 
	  mUl=document.getElementById(mArr[i].ID);	
	  
	}
}

//判断是否需要滚动	
if(navWidth>pageWidth){

	mUl.style.marginLeft=origLeft;
	//navWidth=navWidth+42;
	mUl.style.width=String(navWidth)+"px";	
	document.getElementById('leftBt').style.display="";
	document.getElementById('rightBt').style.display="";
	scrollFlag=1;
}
else{
	mUl.style.left="0px";
	document.getElementById('leftBt').style.display="none";
	document.getElementById('rightBt').style.display="none";	
	scrollFlag=0;
}
}
//切换菜单时获取宽度
function regetWidth(_id){
var i=_id;
theId=i;
var tmp=mObj.childNodes.item(i);
var tmpLen;
var tmpNode;
var nodeWidth=0;		
mArr[i].DISPLAY="";
pageWidth=$(window).width();

if(mArr[i].DISPLAY!="none"){

  var tmpLen=document.getElementById(mArr[i].ID).childNodes.length
  var tmpNode=document.getElementById(mArr[i].ID).childNodes;
  //计算图标菜单宽度
		
  for(var j=0;j<tmpLen;j++){			
	 nodeWidth+=tmpNode.item(j).offsetWidth;
	 
  }
  nodeWidth+=marginLeftWidth;
  mArr[i].WIDTH=nodeWidth+marginLeftWidth;
  navWidth=mArr[i].WIDTH;
  maxWidth=mArr[i].WIDTH-pageWidth;	 
  mUl=document.getElementById(mArr[i].ID);
  
}	
//判断是否需要滚动	
if(navWidth>pageWidth){
	mUl.style.marginLeft=origLeft;
	//navWidth=navWidth+42;
	mUl.style.width=String(navWidth)+"px";	
	document.getElementById('leftBt').style.display="";
	document.getElementById('rightBt').style.display="";
	scrollFlag=1;
}
else{
	mUl.style.left="0px";
	document.getElementById('leftBt').style.display="none";
	document.getElementById('rightBt').style.display="none";
	scrollFlag=0;
}
}
//箭头标签
function bulidArrow(){

var leftArrow=document.createElement("div");	
var rightArrow=document.createElement("div");   
var leftA=document.createElement("a")
var rightA=document.createElement("a");
leftA.setAttribute('href','javascript:void(0)');
rightA.setAttribute('href','javascript:void(0)');
leftArrow.appendChild(leftA);
rightArrow.appendChild(rightA);
leftArrow.className="leftArrow";
rightArrow.className="rightArrow";
leftArrow.setAttribute('id','leftBt');
rightArrow.setAttribute('id','rightBt');
document.getElementById("InMenuBg").insertBefore(leftArrow,document.getElementById("InMenuBg").childNodes[0]) 	//左箭头
document.getElementById("InMenuBg").appendChild(rightArrow);	//右箭头
document.execCommand("BackgroundImageCache", false, true);	//ie 6 背景图片缓存控制

leftArrow.style.display="none";
rightArrow.style.display="none";

leftArrow.onclick=function(){
	tarDis=mUl.style.pixelLeft+step;
	leftAnime("click");
}
//leftArrow.onmouseover=function(){
	//leftAnime("press");
	
//}
//leftArrow.onmouseout=function(){
	//clearTimeout(animeTimer);
	
//}

rightArrow.onclick=function(){		
	tarDis=mUl.offsetLeft-step;
	rightAnime("click");						
}
//rightArrow.onmouseover=function(){
	//rightAnime("press");	
//}
//rightArrow.onmouseout=function(){
	//clearTimeout(animeTimer);				
//}
}

/** 动画事件
/* leftAnime, rightAnime
**/
function leftAnime(_state){
if(_state=="press"){ //按住时持续动画
	if(mUl.style.pixelLeft>=0){					
			clearTimeout(animeTimer);
			mUl.style.marginLeft=origLeft;
		}
		else{				
			mUl.style.marginLeft=String(mUl.style.pixelLeft++)+"px";
			animeTimer=setTimeout("leftAnime('press')", second);
	}
}
else if(_state="click"){ //点击时一次动画
	if(mUl.style.pixelLeft>=0){			
		clearTimeout(animeTimer);
		mUl.style.marginLeft=origLeft;
	}
	else{
		if(mUl.style.pixelLeft<tarDis){
			mUl.style.marginLeft=String(mUl.style.pixelLeft++)+"px";
			animeTimer=setTimeout("leftAnime('click')", second);
		}
		else{
			clearTimeout(animeTimer);
		}
	}	
}
else{
	clearTimeout(animeTimer);
}
}
function rightAnime(_state){
if(_state=="press"){ //按住时持续动画

	if(mUl.offsetLeft<=-maxWidth){		
		clearTimeout(animeTimer);								
	}	
	else{
		mUl.style.marginLeft=String(mUl.style.pixelLeft--)+"px";
		animeTimer=setTimeout("rightAnime('press')", second);			
	}
}
else if(_state="click"){ //点击时一次动画
	if(mUl.offsetLeft<=-maxWidth){
		clearTimeout(animeTimer);			
	}
	else{
		if(mUl.offsetLeft>tarDis){
			mUl.style.marginLeft=String(mUl.style.pixelLeft--)+"px";
			animeTimer=setTimeout("rightAnime('click')", second);
		}
		else{
			clearTimeout(animeTimer);
		}
	}
}
else{
	clearTimeout(animeTimer);
}
}	

/**滚动菜单结束**/
/**展开收起**/
function TopOpenClose(){
	$('#TopOC').bind('click',function(){
		$('.in_menu_bg').toggle();
		switchUpDown();	
		$('.logo_pic').toggle();
		$('.logo_pic2').toggle();
	});
}
/**更换箭头样式**/
function switchUpDown(){
	if($('.in_menu_bg').css('display')=='none'){
		$('#TopOC').find('a').addClass('down');
	}
	else{
	$('#TopOC').find('a').removeClass('down');
	}	
	setFrameHeight();
}